//
// Created by feng on 19-3-31.
// PS: Define the rectangle receiver data structure.
//

#ifndef SOLARENERGY_CHIER_RECTANGLE_RECEIVER_CUH
#define SOLARENERGY_CHIER_RECTANGLE_RECEIVER_CUH

#include "CudaReceiver.cuh"
#include <algorithm/rayTracing/qmcrt_util/global_function.cuh>

namespace solar::cuda
{
    class CudaRectangleReceiver : public CudaReceiver
    {
        public:
            __device__ __host__ CudaRectangleReceiver() = default;

            CudaRectangleReceiver(const CudaRectangleReceiver& rectangle_receiver);

            // TODO: ADD TESTS
            __device__ __host__ auto GIntersect(const float3& origin, const float3& dir, float& t,
                                                float& u, float& v) -> bool
            {
                return global_func::rayParallelogramIntersect(
                    origin, dir, rect_vertexes_[0], rect_vertexes_[1], rect_vertexes_[3], t, u, v);
            }

            void CInit(float pixel_per_meter_for_receiver) override;

            auto getFocusCenter(const float3& heliostat_position) -> float3 override;

            void setFocusCenter();

            [[nodiscard]] auto getRectangleVertex(int index) const -> float3;
            [[nodiscard]] auto getLocalNormal() const -> float3;

            float3 rect_vertexes_[4]; // Four vertexes of rectangle
            float3 local_normal_;     // 局部法向-----标示了receiver的朝向
            float3 focus_center_;     // 聚焦点，为接收器的中心

            
            void Cset_resolution(float pixel_per_meter_for_receiver) override;
            // Internal use only
            void asImagePlane(const float3& new_x_axis);
        private:
            void Cinit_vertex();
            void Cset_local_normal(); // Set local normal
            void Cset_local_vertex(); // Set local vertex
            void Cset_world_vertex(); // Set world vertex
            void setWorldVertexAsImagePlane(const float3& new_x_axis);
    };
} // namespace solar::cuda

#endif // SOLARENERGY_CHIER_RECTANGLE_RECEIVER_CUH
